# AUTHOR: DING
# -*- codeing = utf-8 -*-
# @Time: 2024/2/26 19:16
# @Author: 86139
# @Site: 
# @File: 18-network.py
# @Software: PyCharm
# tensorboard --logdir=pytorch/logs --port=6007

import torch
from torch import nn
import torchvision
from torch.utils.data import DataLoader

dataset = torchvision.datasets.CIFAR10("./dataset", train=False, transform=torchvision.transforms.ToTensor(),
                                       download=True)
dataloader = DataLoader(dataset, batch_size=64, drop_last=True)


class MyModule(nn.Module):
    def __init__(self):
        super().__init__()
        self.model1 = nn.Sequential(
            nn.Conv2d(3, 32, (5, 5), padding=2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 32, (5, 5), padding=2),
            nn.MaxPool2d(2),
            nn.Conv2d(32, 64, (5, 5), padding=2),
            nn.MaxPool2d(2),
            nn.Flatten(),
            nn.Linear(1024, 64),
            nn.Linear(64, 10))

    def forward(self, x):
        return self.model1(x)


network = MyModule()
loss = nn.CrossEntropyLoss()
for data in dataloader:
    imgs, targets = data
    output = network(imgs)
    res = loss(output, targets)
    print(res)
    res.backward()

